package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Slf4j
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    // 套餐列表
    @Override
    public Page<Setmeal> findByPage(Integer pageNum, Integer pageSize, String name) {
        // 1.套餐条件分页查询
        // 1-1 构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name), Setmeal::getName, name);
        //开启分页
        Page<Setmeal> page = new Page<>(pageNum, pageSize);
        //mapper查询
        page = setmealMapper.selectPage(page, qw);
        for (Setmeal setmeal : page.getRecords()) {
            // 2. 根据category_id 查询并封装分类名称
            Category category = categoryMapper.selectById(setmeal.getCategoryId());
            //根据id查询并封装菜品
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<SetmealDish>();
            wrapper.eq(SetmealDish::getSetmealId, setmeal.getId());
            List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            setmeal.setSetmealDishes(setmealDishList);

        }
        return page;
    }

    @Override
    public void save(Setmeal setmeal) {
        // 1.新增套餐
        setmealMapper.insert(setmeal);
        log.info("套餐主键返回：" + setmeal.getImage());
        //2遍历前端提交套餐菜品列表
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishList) {
            // 关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            // 保存中间表
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void deleteBactch(List<Long> ids) {
        // 1.先查询套餐状态，是否有启售 select count(*) from setmeal where status=1 and id in(1,3,5);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.in(Setmeal::getId, ids);//id in(1,3,5)
        qw.eq(Setmeal::getStatus, 1);//status=1
        Integer count = setmealMapper.selectCount(qw);
        if (count > 0) {
            throw new CustomException("套餐中有启售状态，请先停售再删除~~~");
        }
        //删除套餐}
        setmealMapper.deleteBatchIds(ids);
        //  // 3.删除套餐内菜品中间表
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SetmealDish::getSetmealId, ids);
        setmealDishMapper.delete(wrapper);


    }

    @Override
    public void updatestatus(Integer status, List<Long> ids) {
        //条件对象
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId, ids);
        //修改实体
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        //调用mapper
        setmealMapper.update(setmeal, wrapper);
    }

    /*  @Override
      public Dish findById(long id) {
          //先查菜品
          Dish dish = dishMapper.selectById(id);
          // 2.根据菜品id查询口味
          // 2-1 构建条件
          LambdaQueryWrapper<DishFlavor>qw=new LambdaQueryWrapper<>();
          qw.eq(DishFlavor::getDishId,id);
          // 2-2  调用mapper
          List<DishFlavor>dishFlavorList=dishFlavorMapper.selectList(qw);
          //3.将口味设置到菜品
          dish.setFlavors(dishFlavorList);
          //  4.返回菜品
          return dish;
      }*/
    @Override
    public Setmeal findById(long id) {
        //先查套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        // 2.根据套餐id查询菜品
        // 2-1 构建条件
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, id);
        // 2-2  调用mapper
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(qw);
        //3.将菜品设置到套餐
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }

    /*  @Override
      public void updateById(Dish dish) {
          //1修改菜品基本信息
          dishMapper.updateById(dish);
          //根据菜品id删除原来口味
          LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
          qw.eq(DishFlavor::getDishId,dish.getId());
          dishFlavorMapper.delete(qw);
          //遍历新增口味
          for (DishFlavor flavor : dish.getFlavors()) {
              // 关联菜品id
              flavor.setDishId(dish.getId());
              // 口味新增
              dishFlavorMapper.insert(flavor);
          }*/
    //修改套餐
    @Override
    public void updateById(Setmeal setmeal) {
        //1修改套餐基本信息
        setmealMapper.updateById(setmeal);
        //根据套餐id删除原来菜品
        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getDishId, setmeal.getId());
        setmealDishMapper.delete(qw);
        //遍历新增菜品
        for (SetmealDish setmealDish : setmeal.getSetmealDishes()) {
            // 关联套餐id
            setmealDish.setSetmealId(setmeal.getId());
            // 菜品新增
            setmealDishMapper.insert(setmealDish);
        }


    }

    //套餐列表
    @Override
    public List<Setmeal> findlist(Long categoryId, Integer status) {
        //1.构建条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);//比较分类id
        qw.eq(Setmeal::getStatus,status);//比较套餐启售状态
    /*
    //调用mapper查询
    List<Setmeal> setmealList = setmealMapper.selectList(qw);
     */

        //2.执行查询并返回
        //return setmealList;
        return setmealMapper.selectList(qw);
    }

    @Override
    public List<Dish> getmealDish(Long id) {
        //1. 从中间表获取菜品信息
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);

        //2. 创建一个集合用于封装返回菜品信息
        List<Dish> dishList = new ArrayList<>();

        //3. 遍历得到菜品列表
        for (SetmealDish setmealDish : setmealDishList) {
            //根据菜品id从菜品表查询菜品详情
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            dish.setCopies(setmealDish.getCopies());//设置份数
            dishList.add(dish);//添加到返回集合
        }

        //4. 返回
        return dishList;

    }


}